package com.seleniti.enpareja.web.desktop.client.ui.view;

import org.ioc.commons.impl.gwt.client.ui.HasEnabledWrapper;
import org.ioc.commons.impl.gwt.client.ui.HasVisibilityWrapper;
import org.ioc.commons.impl.gwt.client.ui.ViewComposite;
import org.ioc.commons.ui.HasValue;
import org.ioc.commons.ui.HasVisibility;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;
import com.seleniti.enpareja.controller.ui.view.MainView;
import com.seleniti.enpareja.controller.ui.view.MainView.Action;
import com.seleniti.enpareja.controller.ui.view.MainView.Event;
import com.seleniti.enpareja.controller.ui.view.MainView.Operation;
import com.seleniti.enpareja.controller.ui.view.MainView.Presenter;
import com.seleniti.enpareja.controller.ui.view.MainViewPresenter;
import com.seleniti.enpareja.web.desktop.client.ui.widget.PendingMark;

public class MainViewWidget extends EnParejaBaseView<Event, Operation, Action, Presenter> implements MainView {

	private static MainViewWidgetUiBinder uiBinder = GWT.create(MainViewWidgetUiBinder.class);

	interface MainViewWidgetUiBinder extends UiBinder<Widget, MainViewWidget> {
	}

	private boolean enabled;

	@UiField
	Button btnShoppingList;

	@UiField
	Button btnHomeTasks;

	@UiField
	Button btnMeals;

	@UiField
	Button btnWorkShifts;

	@UiField
	Button btnGarage;

	@UiField
	Button btnPendingAtHome;

	@UiField
	Button btnSharedCost;

	@UiField
	Button btnImportantDates;

	@UiField
	Panel configUserSuggestionPanel;
	
	@UiField
	PendingMark shoppingListPendingMark;

	@UiField
	PendingMark homeTasksPendingMark;

	@UiField
	PendingMark productsPendingMark;

	public MainViewWidget() {
		this.presenter = new MainViewPresenter(this, eventbus, operationManager, actionController);
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public boolean isEnabled() {
		return this.enabled;
	}

	@Override
	public void setEnabled(boolean enabled) {
		this.enabled = true;
		HasEnabledWrapper.setEnabled(enabled, btnMeals, btnGarage, btnHomeTasks, btnPendingAtHome, btnShoppingList,
				btnWorkShifts, btnSharedCost, btnImportantDates);
		this.setTitle(enabled ? "" : "¡Asociate a tu pareja antes de empezar a usar todas las opciones!");
	}

	@UiHandler("btnShoppingList")
	void onShoppingListButtonClicked(ClickEvent evt) {
		this.presenter.onGoToShoppingListView();
	}

	@UiHandler("btnHomeTasks")
	void onHomeTasksButtonClicked(ClickEvent evt) {
		this.presenter.onGoToHomeTasksView();
	}

	@UiHandler("btnMeals")
	void onMealsButtonClicked(ClickEvent evt) {
		this.presenter.onGoToMealsView();
	}

	@UiHandler("btnWorkShifts")
	void onWorkShiftsButtonClicked(ClickEvent evt) {
		this.presenter.onGoToWorkShiftsView();
	}

	@UiHandler("btnSharedCost")
	void onSharedCostButtonClicked(ClickEvent evt) {
		this.presenter.onGoToSharedCostView();
	}

	@UiHandler("btnPendingAtHome")
	void onPendingAtHomeButtonClicked(ClickEvent evt) {
		this.presenter.onGoToPendingShoppingView();
	}

	@Override
	public HasVisibility configUserSuggestionPanelVisibility() {
		return HasVisibilityWrapper.from(configUserSuggestionPanel, this);
	}

	@UiHandler("btnGoSettings")
	void onGoSettingsButtonClicked(ClickEvent evt) {
		this.presenter.onGoSettings();
	}

	@Override
	public HasValue<Integer> pendingShoppingList() {
		return this.shoppingListPendingMark;
	}

	@Override
	public HasValue<Integer> pendingHomeTasks() {
		return this.homeTasksPendingMark;
	}

	@Override
	public HasValue<Integer> pendingProducts() {
		return this.productsPendingMark;
	}
}
